package com.graduation.smartparkingplatform.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Dao.UserMapper;
import com.graduation.smartparkingplatform.Pojo.User;
import com.graduation.smartparkingplatform.Pojo.UserInfo;
import com.graduation.smartparkingplatform.Pojo.UserNA;
import com.graduation.smartparkingplatform.Service.UserService;
import com.graduation.smartparkingplatform.Utils.IPAddressUtils;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
  @Autowired UserMapper userMapper;

  // 登录验证
  @Override
  public User toLogin(String username, String password) {
    return userMapper.findUser(username, password);
  }

  // 登录日志
  @Override
  @Async
  public Boolean visitorLog(Integer userId, String username, String ipAddress, String agent) {
    // 解析IP地址城市
    IPAddressUtils ip = new IPAddressUtils();
    ip.init();
    // 获取ip城市
    String city = ip.getIPLocation(ipAddress).getCountry();

    // 解析agent字符串
    UserAgent userAgent = UserAgent.parseUserAgentString(agent);
    // 获取浏览器对象
    Browser browser = userAgent.getBrowser();
    OperatingSystem operatingSystem = userAgent.getOperatingSystem(); // 获取操作系统对象
    String device = operatingSystem.getDeviceType().toString(); // 访问设备类型
    String browsers = browser.getManufacturer().toString(); // 浏览器生产厂商

    String operation = "登录";
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    return userMapper.visitorLog(
        userId, username, operation, ipAddress, city, device, browsers, nowTime);
  }

  // 获取User列表（分页）
  @Override
  public PageInfo<UserNA> getUserList(Integer page, Integer count) {
    PageHelper.startPage(page, count);

    List<UserNA> userNAList = userMapper.getUserList();

    for (UserNA userNA : userNAList) {
      if (userNA.getCompany_name() == null) {
        userNA.setCompany_name("无");
      }
    }

    PageInfo<UserNA> pageInfo = new PageInfo<>(userNAList);

    return pageInfo;
  }

  // 模糊搜索User（分页）
  @Override
  public PageInfo<UserNA> searchUser(Integer page, Integer count, String userName) {
    PageHelper.startPage(page, count);

    List<UserNA> userNAList = userMapper.searchUser(userName);

    PageInfo<UserNA> pageInfo = new PageInfo<>(userNAList);

    return pageInfo;
  }

  // 获取用户信息
  @Override
  public UserNA getUserMessage(Integer userId) {
    return userMapper.getUserMessage(userId);
  }

  // 添加用户
  public Boolean addUser(User user) {
    Integer orgId = user.getOrg_id();
    String userName = user.getUsername();
    String password = user.getPassword();
    String nickName = user.getNickname();
    String telphone = user.getTelphone();
    String email = user.getEmail();
    Boolean admin = user.getAdmin();
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    return userMapper.addUser(orgId, userName, password, nickName, telphone, email, admin, nowTime);
  }

  // 修改用户信息
  @Override
  public Boolean changeInfo(UserInfo userInfo) {
    Integer userId = userInfo.getUserId();
    User user = userInfo.getUser();
    Integer orgId = user.getOrg_id();
    String userName = user.getUsername();
    String password = user.getPassword();
    String nickName = user.getNickname();
    String telphone = user.getTelphone();
    String email = user.getEmail();
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    return userMapper.changeInfo(
        userId, orgId, userName, password, nickName, telphone, email, nowTime);
  }

  // 修改用户状态
  @Override
  public Boolean changeUserStatus(Integer userId, Boolean status) {
    System.out.println(userMapper.changeUserStatus(userId, status));
    return userMapper.changeUserStatus(userId, status);
  }
  //  修改用户管理员权限
  @Override
  public Boolean changeUserAdmin(Integer userId, Boolean adminStatuts) {
    return userMapper.changeUserAdmin(userId, adminStatuts);
  }

  // 删除用户
  public Boolean deleteUser(Integer userId) {
    return userMapper.deleteUser(userId);
  }
}
